import json
from connect_to_redis import BaseRedisConnection

import redis

# String常用命令
# set:设置值
# get:获取值
# mset:设置多个键值对
# mget:获取多个键值对
# append:添加字符串
# incr/decr:增加/减少1

class TestString(object):
    def __init__(self):
        self.conn = redis.Redis(
            host='localhost',
            port=6379,
            db=1,
            decode_responses=True
        )


    def test_set(self):
        # set 设置值
        user1 = self.conn.set('user1','Amy')
        print(user1)
        user2 = self.conn.set('user2', '张三')
        print(user2)
        # 10秒后自动删除
        user3 = self.conn.set('user3','user3',ex=10)
        print(user3)
    def test_get(self):
        # get 获取值
        user1 = self.conn.get('user1')
        print('user1：',user1)
        user2 = self.conn.get('user2')
        # user2 = self.conn.get('user2').decode('utf-8')
        print('user2：', user2)
        user3 = self.conn.get('user3')
        print('user3：', user3)
        user4 = self.conn.get('user4')
        print('user4：', user4)


    def test_mset(self):
        # mset 设置多个键值对
        d = {
            'user5':'Bob',
            'user6': 'Bobx'
        }
        result = self.conn.mset(d)
        print(result)


    def test_mget(self):
        # mget 获取多个键值对
        result = self.conn.mget(['user1','user5','user7'])
        print(result)


    def test_incr(self):
        # incr/decr 增加/减少1
        result = self.conn.incr('age')
        print(result)
        age = self.conn.get('age')
        print('age:',age)


    def test_del(self):
        result = self.conn.delete('user1')
        print(result)


    # 模拟用户注册
    def register(self,username,password,nickname):
        key = f'user:{username}'
        user_info = {
            'username':username,
            'password':password,
            'nickname':nickname
        }
        value = json.dumps(user_info)
        result = self.conn.set(key,value)
        print('result',result)

    def login(self,username,password):
        key = f'user:{username}'
        result = self.conn.get(key)
        print('result', result)
        if result is None:
            print('用户不存在')
            return None
        user_info = json.loads(result)
        print(user_info['username'])
        print(user_info['password'])
        print(user_info['nickname'])
        if user_info['password'] != password:
            print('密码输入')
            return None
        print('登录成功')


    def __del__(self):
        try:
            self.conn.close()
        except Exception as e:
            print(e)


class TestString1(BaseRedisConnection):
    def test_set(self):
        # set 设置值
        user1 = self.conn.set('user1','Amy')
        print(user1)
        user2 = self.conn.set('user2', '张三')
        print(user2)
        # 10秒后自动删除
        user3 = self.conn.set('user3','user3',ex=10)
        print(user3)
    def test_get(self):
        # get 获取值
        user1 = self.conn.get('user1')
        print('user1：',user1)
        user2 = self.conn.get('user2')
        # user2 = self.conn.get('user2').decode('utf-8')
        print('user2：', user2)
        user3 = self.conn.get('user3')
        print('user3：', user3)
        user4 = self.conn.get('user4')
        print('user4：', user4)



def main():
    str_obj = TestString()
    str_obj1 = TestString1()
    str_obj1.test_get()
    # str_obj.test_set()
    # str_obj.test_get()
    # str_obj.test_mset()
    # str_obj.test_mget()
    # str_obj.test_incr()
    # str_obj.test_del()
    # str_obj.register('lili','123456','丽丽')
    # str_obj.login('lili','123456')


if __name__ == '__main__':
    main()